import hashlib
import logging
import os
import shutil
import sys
import unittest

import check
import util


__author__  = 'ZL <837806295@qq.com>'
__product__ = 'test'
__version__ = '3.0.0'
__date__    = '2023/04/08'


log = logging.getLogger(__product__)


class TestCase001(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_command(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase002(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

        shutil.copytree(
            util.get_test_subdir(__file__, ['data', 'file']),
            os.path.join(cls.work_dir, 'file'))

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_subcommand_create(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" create -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_create2(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" create "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file', 'case01.jpeg')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_create3(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" create -f -m 7 "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file', 'case01.jpeg')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_create4(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" create -f "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_create5(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" create -f -m 9 "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_create6(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" create -f -e "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_create7(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" create -f -e -m 6 "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_create8(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" create "%s" -s %s'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file'),
                    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
                    'xxxxxxxxxxxxxxxx'
                )
        )

        self.assertFalse(util.run(cmd))

    def test_subcommand_create9(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" create "%s" -s %s'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file'),
                    'abc'
                )
        )

        self.assertFalse(util.run(cmd))

class TestCase003(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_subcommand_list(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list2(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list3(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -c "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list4(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -e -c "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list5(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r digest "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list6(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r digest -c "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list7(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r no_digest "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list8(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r no_digest -c "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list9(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r both "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list10(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r both -c "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list11(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r extension "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list12(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r extension -c "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list13(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r special "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list14(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r special -c "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list15(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r empty "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_list16(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" list -r empty -c "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'digest_file'])
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase004(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

        shutil.copytree(
            util.get_test_subdir(__file__, ['data', 'digest_file']),
            os.path.join(cls.work_dir, 'digest_file'))

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_subcommand_remove(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" remove -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_remove2(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" remove "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'digest_file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_remove3(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" remove -e "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'digest_file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_remove4(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" remove -t "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'digest_file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_remove5(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" remove -e -t "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'digest_file')
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase005(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

        shutil.copytree(
            util.get_test_subdir(__file__, ['data', 'file']),
            os.path.join(cls.work_dir, 'file'))

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_subcommand_check(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" check -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_check2(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" check "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file', 'case01.jpeg')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_check3(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" check -m 5 "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file', 'case01.jpeg')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_check4(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" check "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_check5(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" check -m 5 "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_check6(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" check -m 5 -e "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.work_dir, 'file', 'case01.jpeg')
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase101(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_range(self):
        self.assertFalse(set([3, 2, 1, 0]) ^ set(range(3, -1, -1)))
        self.assertFalse(set([]) ^ set(range(-1, -1, -1)))

    def test_slice(self):
        case01 = '/abc/def/xd.info'
        self.assertEqual(case01[: len(case01) - len('.info')], '/abc/def/xd')

    def test_os_walk(self):
        def on_error(e):
            self.assertEqual(e.filename, '/abc/def')

        for _, _, _ in os.walk('/abc/def', True, on_error):
            pass

    def test_hashlig_sha512(self):
        m = hashlib.sha512()
        m.update(b'')
        self.assertNotEqual(m.hexdigest(), '')

class TestCase102(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_digest(self):
        d = check.Digest(b'123')
        d.append_data(b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')

        d2 = check.Digest(b'123')
        d2.append_data(b'abcdefghijklmnopqrstuvwxyz')
        d2.append_data(b'ABCDEFGHIJKLMNOPQRSTUVWXYZ')

        d3 = check.Digest(b'abc')
        d3.append_data(b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')

        self.assertTrue(isinstance(d.digest(), str))
        self.assertEqual(len(d.digest()), 64)
        self.assertEqual(d.digest(), d2.digest())
        self.assertNotEqual(d.digest(), d3.digest())

    def test_is_supported_key(self):
        self.assertTrue(
            check.is_supported_key(
                '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
                '`~!@#$%^&*()-_=+[{}]\\|;:\'",<.>/? '))

        self.assertFalse(check.is_supported_key('我是谁'))

    def test_has_extension(self):
        self.assertTrue(util.has_extension('abc.info', '.INFO'))
        self.assertTrue(util.has_extension('/abc/abc.info', '.INFO'))

        self.assertFalse(util.has_extension('', ''))
        self.assertFalse(util.has_extension('abc.info', ''))
        self.assertFalse(util.has_extension('', '.INFO'))
        self.assertFalse(util.has_extension('abc.info', '.TXT'))

    def test_generate_digest_path(self):
        self.assertEqual(check.generate_digest_path('/abc/abc.mp4'),
                         '/abc/abc.mp4.info')
        self.assertEqual(check.generate_digest_path('abc.mp4'),
                         'abc.mp4.info')
        self.assertEqual(check.generate_digest_path('abc.info'),
                         'abc.info.info')

    def test_is_hide(self):
        self.assertTrue(util.is_hide('.'))
        self.assertTrue(util.is_hide('..'))
        self.assertTrue(util.is_hide('.bash_history'))
        self.assertTrue(util.is_hide('.abc.txt'))
        self.assertTrue(util.is_hide('.git'))

        self.assertFalse(util.is_hide(''))
        self.assertFalse(util.is_hide('abc'))
        self.assertFalse(util.is_hide('abc.txt'))

    def test_strip_hide_dir(self):
        self.assertFalse(
            set(util.strip_hide_dir('/abc', ['.', '..', '.git', 'abc', 'def']))
            ^ set(['abc', 'def']))
        self.assertFalse(
            set(util.strip_hide_dir('/abc', ['.', '..', '.git'])) ^ set())
        self.assertFalse(
            set(util.strip_hide_dir('/abc', [])) ^ set())

    def test_is_digest_file(self):
        self.assertTrue(
            check.is_digest_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case01.info')))
        self.assertTrue(
            check.is_digest_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case02.info')))

        self.assertFalse(
            check.is_digest_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case00.info')))
        self.assertFalse(
            check.is_digest_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case03.info')))
        self.assertFalse(
            check.is_digest_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case04.txt')))
        self.assertFalse(
            check.is_digest_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case05.info')))
        self.assertFalse(
            check.is_digest_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case06.info')))
        self.assertFalse(
            check.is_digest_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case07.info')))
        self.assertFalse(
            check.is_digest_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case08.info')))

    def test_get_digest(self):
        self.assertTrue(
            check.get_digest(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case01')))

        self.assertFalse(
            check.get_digest(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case00')))
        self.assertFalse(
            check.get_digest(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case02')))
        self.assertFalse(
            check.get_digest(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    'case04')))

    def test_choose_requested_files(self):
        self.assertFalse(
            set(check.choose_requested_files(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    [
                        '.abc',
                        '.abc.txt',
                        '.info',
                        'case01',
                        'case01.info',
                        'case04',
                        'case04.txt',
                        'case05',
                        'case05.info'
                    ],
                    True
                )
            )
            ^ set(
                [
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case01'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case04'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case04.txt'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case05'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case05.info')
                ]
            )
        )

        self.assertFalse(
            set(check.choose_requested_files(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    [
                        '.abc',
                        '.abc.txt',
                        '.info',
                        'case01',
                        'case01.info',
                        'case04',
                        'case04.txt',
                        'case05',
                        'case05.info'
                    ],
                    False
                )
            )
            ^ set(
                [
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        '.abc'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        '.abc.txt'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        '.info'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case01'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case04'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case04.txt'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case05'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case05.info')
                ]
            )
        )

        self.assertFalse(
            set(check.choose_requested_files(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    [],
                    False))
            ^ set())

    def test_choose_file_by_extension(self):
        self.assertFalse(
            set(util.choose_file_by_extension(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    [
                        '.abc',
                        '.abc.txt',
                        '.info',
                        'case01',
                        'case01.info',
                        'case04',
                        'case04.txt',
                        'case05',
                        'case05.info'
                    ],
                    True,
                    '.info'
                )
            )
            ^ set(
                [
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case01.info'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case05.info')
                ]
            )
        )

        self.assertFalse(
            set(util.choose_file_by_extension(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    [
                        '.abc',
                        '.abc.txt',
                        '.info',
                        'case01',
                        'case01.info',
                        'case04',
                        'case04.txt',
                        'case05',
                        'case05.info'
                    ],
                    False,
                    '.info'
                )
            )
            ^ set(
                [
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        '.info'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case01.info'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'digest_file']),
                        'case05.info')
                ]
            )
        )

        self.assertFalse(
            set(util.choose_file_by_extension(
                    util.get_test_subdir(__file__, ['data', 'digest_file']),
                    [],
                    False,
                    '.info'))
            ^ set())

    def test_count_file_by_extension(self):
        self.assertEqual(
            util.count_file_by_extension(
                util.get_test_subdir(__file__, ['data', 'digest_file']),
                [
                    '.abc',
                    '.abc.txt',
                    '.info',
                    'case01',
                    'case01.info',
                    'case04',
                    'case04.txt',
                    'case05',
                    'case05.info'
                ],
                True,
                '.info'
            ),
            2
        )

        self.assertEqual(
            util.count_file_by_extension(
                util.get_test_subdir(__file__, ['data', 'digest_file']),
                [
                    '.abc',
                    '.abc.txt',
                    '.info',
                    'case01',
                    'case01.info',
                    'case04',
                    'case04.txt',
                    'case05',
                    'case05.info'
                ],
                False,
                '.info'
            ),
            3
        )

        self.assertFalse(
            util.count_file_by_extension(
                util.get_test_subdir(__file__, ['data', 'digest_file']),
                [],
                False,
                '.info'
            ),
            0
        )

class TestCase103(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data', 'file']),
                         'case01.jpeg'),
            os.path.join(cls.work_dir, 'case01.jpeg'))

    @classmethod
    def tearDownClass(cls):
        del cls.work_dir
        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.work_dir = self.__class__.work_dir

    def tearDown(self):
        del self.work_dir

    def test_create_digest_for_file(self):
        self.assertTrue(
            check.create_digest_for_file(
                os.path.join(self.work_dir, 'case01.jpeg'),
                '0123456789abcdef',
                8*1024*1024,
                True))

        self.assertTrue(
            util.compare_files(
                os.path.join(self.work_dir, 'case01.jpeg.info'),
                os.path.join(util.get_test_subdir(__file__, ['data', 'file']),
                             'case01.jpeg.info')))

    def test_check_digest_for_file(self):
        self.assertTrue(
            check.check_digest_for_file(
                os.path.join(
                    util.get_test_subdir(__file__, ['data', 'file']),
                    'case01.jpeg'),
                '0123456789abcdef',
                8*1024*1024))

class UnorderedTestLoader(unittest.TestLoader):
    sortTestMethodsUsing = lambda self, x, y: -1


if __name__ == '__main__':
    util.set_log(__file__, '%s_v%s' % (__product__, __version__), 'debug')

    log.info('program version: %s %s', __product__, __version__)
    log.debug('command line: %s', sys.argv)

    unittest.main(testLoader=UnorderedTestLoader())
